//
//  ZYCCircleFriendsColletionView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/23.
//

#import "ZYCCircleFriendsColletionView.h"
#import "ZYCCircleFriendsColletionCell.h"
#import "ZYCCycleFriendDetailModel.h"

@interface ZYCCircleFriendsColletionView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, assign) NSUInteger currentPage;

@end
@implementation ZYCCircleFriendsColletionView

-(instancetype) initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    if (self = [super initWithFrame:frame collectionViewLayout:self.flowLayout]) {
        [self setup];
    }
    return self;
}

-(instancetype) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

-(instancetype) initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
#pragma mark - private method
-(void) setup{
    self.delegate = self;
    self.dataSource = self;
    self.pagingEnabled = YES;
    self.showsHorizontalScrollIndicator = NO;
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.collectionViewLayout = self.flowLayout;
    [self registerClass:[ZYCCircleFriendsColletionCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCCircleFriendsColletionCell class])];

}

#pragma mark - UICollectionViewDelegate
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(__kindof UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZYCCircleFriendsColletionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCCircleFriendsColletionCell class]) forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.dataArray[indexPath.item];
    cell.uploadUserName = self.uploadUserName;
    return cell;

}

-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = scrollView.contentOffset.x / scrollView.frame.size.width;
    if (page != self.currentPage) {
        self.currentPage = page;
        if ([self.pDelegate respondsToSelector:@selector(paperCollectionViewScrollPage:)]) {
            [self.pDelegate paperCollectionViewScrollPage:self.currentPage];
        }
    }
}


#pragma mark - getters
-(UICollectionViewFlowLayout *) flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _flowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight);
        _flowLayout.footerReferenceSize = CGSizeZero;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _flowLayout;
}
@end
